<template>
	<view class="sale-list-page">
		<detail-head url="/pages/mine/mine" type="none"></detail-head>
		<view class="list-type">
			<view :class="['type', active ? 'active' : '' ]" @click="onTypeChange(true)">作品</view>
			<view :class="['type', !active ? 'active' : '' ]" @click="onTypeChange(false)">盲盒</view>
		</view>

		<view class="list" v-show="active">
			<view class="item" v-for="item in prodList" :key="item.id">
				<view class="title">"{{item.worksName}}"</view>
				<view class="right"></view>
				<view class="infos">
					<view class="info-img">
						<image :src="item.subPctureUrl" mode="aspectFit"></image>
					</view>
					<view class="info-other">
						<view class="info">
							<view class="label">出售金额</view>
							<view class="value">￥{{ item.price }}</view>
						</view>
						<view class="info">
							<view class="label">作品编号</view>
							<view class="value" @click="$tool.copyText(item.worksCode)"> {{ setStringStylecode(item.worksCode) }}</view>
						</view>
						<view class="sale-btn">
							<button type="default" @click="goToSalePage(item.id)">出售商品</button>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="list" v-show="!active">
			<view class="item" v-for="item in boxList" :key="item.id">
				<view class="title">"{{item.boxName}}"</view>
				<view class="right"></view>
				<view class="infos">
					<view class="info-img">
						<image :src="item.subPctureUrl" mode="aspectFit"></image>
					</view>
					<view class="info-other">
						<view class="info">
							<view class="label">盲盒状态</view>
							<view class="value">未开启</view>
						</view>
						<view class="info">
							<view class="label">盲盒编号</view>
							<view class="value" @click="$tool.copyText(item.blindBoxCode)">{{setStringStylecode(item.blindBoxCode)}}</view>
						</view>
						<view class="sale-btn">
							<button type="default" @click="goToSalePage(item.id)">出售商品</button>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getNoSellBox,
		getNoSellWork
	} from '../../request/sale-api.js'
	import {
		setStringStylecode
	} from '../../utils/utils.js'
	export default {
		data() {
			return {
				// 作品类型
				active: true,
				// 列表数据
				boxList: [],
				prodList: [],
				setStringStylecode
			}
		},
		created() {
			this.$nextTick(function() {
				console.log(this.config.sale_btn)
				if (!this.config.sale_btn) {
					uni.showToast({
						title: '通道暂时关闭，请耐心等待',
						icon: 'none'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/mine/mine'
						})
					}, 500)
				} else {
					this.onTypeChange(true)
				}
			})
		},
		methods: {
			// 跳转
			goToSalePage(id) {
				if (this.active) {
					this.$tool.navigateTo(`/pages/sale/sale-prod?id=${id}`)
				} else {
					this.$tool.navigateTo(`/pages/sale/sale-box?id=${id}`)
				}
			},

			// 产品切换
			onTypeChange(bool) {
				this.active = bool
				if (bool) {
					if (this.prodList.length === 0) {
						this.onLoadProdListData()
					}
				} else {
					if (this.boxList.length === 0) {
						this.onLoadBoxListData()
					}
				}
			},

			// 获取盲盒数据
			async onLoadBoxListData() {
				uni.showLoading({
					title: '加载中'
				})
				try {
					let {
						code,
						data,
						message
					} = await getNoSellBox()
					if (code === 200) {
						this.boxList.push(...data)
						uni.hideLoading()
					} else {
						uni.hideLoading()
						uni.showToast({
							title: message,
							icon: 'error'
						})
					}
				} catch (err) {
					uni.hideLoading()
					this.$tool.showErr(err)
				}
			},

			// 获取作品数据
			async onLoadProdListData() {
				uni.showLoading({
					title: '加载中'
				})
				try {
					let {
						code,
						data,
						message
					} = await getNoSellWork()
					if (code === 200) {
						this.prodList.push(...data)
						uni.hideLoading()
					} else {
						uni.hideLoading()
						uni.showToast({
							title: message,
							icon: 'error'
						})
					}
				} catch (err) {
					uni.hideLoading()
					this.$tool.showErr(err)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.sale-list-page {
		width: 100%;
		min-height: 100vh;
		background: $login-background-url no-repeat;
		background-size: 100% auto;
		background-position: 0 -60rpx;
	}

	.list-type {
		width: 100%;
		height: 100rpx;
		padding: 0 40rpx;
		margin: 80rpx 0 60rpx 0;
		display: flex;
		align-items: center;
		justify-content: space-around;

		.type {
			width: 120rpx;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 32rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #8D8D8D;
		}

		.active {
			color: #F7DE75;
		}
	}

	.list {
		width: 100%;
		margin-top: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.item {
			width: 686rpx;
			height: 360rpx;
			padding: 50rpx 40rpx 0rpx 40rpx;
			margin-bottom: 30rpx;
			background: $active-back-url no-repeat;
			background-size: 100% 100%;
			background-position: center center;
			position: relative;

			.right {
				width: 40rpx;
				height: 40rpx;
				background: $common-right-url no-repeat;
				background-size: 100% 100%;
				background-position: center center;
				position: absolute;
				top: 60rpx;
				right: 40rpx;
				transform: rotate(3deg);
			}

			.title {
				line-height: 40rpx;
				text-align: center;
				font-size: 14px;
				font-family: PingFangSC-Semibold, PingFang SC;
				font-weight: 600;
				color: #9D9D9D;
				margin-bottom: 20rpx;
			}

			.infos {
				width: 610rpx;
				max-height: 230rpx;
				padding-top: 10rpx;
				border-top: 4rpx solid #979797;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.info-img {
					width: 190rpx;
					margin-top: 10rpx;

					image {
						width: 190rpx;
						height: 190rpx;
					}
				}

				.info-other {
					flex: 1;
					height: 200rpx;
					max-width: 420rpx;
					max-height: 200rpx;
					padding-left: 10rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: space-between;

					.info {
						width: 100%;
						height: 48rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;

						.label {
							width: 120rpx;
							font-size: 24rpx;
							font-family: PingFangSC-Medium, PingFang SC;
							font-weight: 500;
							color: #979797;
						}

						.value {
							flex: 1;
							max-width: 320rpx;
							text-align: right;
							font-size: 28rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #979797;
						}
					}

					.sale-btn {
						width: 100%;
						padding: 20rpx 0 0 0;
						border-top: 4rpx solid #979797;
						display: flex;
						justify-content: flex-end;

						button {
							width: 200rpx;
							height: 50rpx;
							margin: 0;
							line-height: 45rpx;
							background: #121212;
							box-shadow: -10px -10px 20px 0px rgba(39, 39, 39, 0.54), 10px 10px 20px 0px rgba(0, 0, 0, 0.83);
							border-radius: 22px;
							border: 1px solid #FFBD47;
							font-size: 14px;
							font-family: SourceHanSansCN-Regular, SourceHanSansCN;
							font-weight: 400;
							color: #FFBD47;
						}
					}
				}
			}

		}
	}
</style>
